/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.graph;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides the implementation of text-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Text {

    /**
     * No instance of this class.
     */
    private Text() {
    } // end empty constructor

    /**
     * Changes the font to a new one, keeping size.
     * @param ctxt context
     * @param fnt new font name
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_set_font(final CodeRunner ctxt,
                                         final Value fnt)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final Font f = slot.getFont();
        slot.setFont(new Font(fnt.asBlock().asString(),
                              Font.PLAIN,
                              f.getSize()));
        return Value.UNIT;
    } // end method 'caml_gr_set_font(CodeRunner, Value)'

    /**
     * Changes the text size.
     * @param ctxt context
     * @param sz new font size
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_set_text_size(final CodeRunner ctxt,
                                              final Value sz)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final Font f = slot.getFont();
        slot.setFont(f.deriveFont((float) sz.asLong()));
        return Value.UNIT;
    } // end method 'caml_gr_set_text_size(CodeRunner, Value)'

    /**
     * Draws a character at current position. <br/>
     * The current position is incremented by the width of the character.
     * @param ctxt context
     * @param ch character to write
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_draw_char(final CodeRunner ctxt,
                                          final Value ch)
        throws Fail.Exception {
        final Block s = Block.createString(1);
        s.setUnsignedByte(0, ch.asLong() & 0xFF);
        return caml_gr_draw_string(ctxt, Value.createFromBlock(s));
    } // end method 'caml_gr_draw_char(CodeRunner, Value)'

    /**
     * Draws a string at current position. <br/>
     * The current position is incremented by the width of the string.
     * @param ctxt context
     * @param str string to write
     * @return <i>unit</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_draw_string(final CodeRunner ctxt,
                                            final Value str)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final int x = slot.getX();
        final int y = slot.convertY(slot.getY());
        final String s = str.asBlock().asString();
        final Graphics2D win = slot.createWindowGraphics();
        if (win != null) {
            win.drawString(s, x, y);
            win.dispose();
            slot.repaint();
        } // end if
        final Graphics2D store = slot.createBackingStoreGraphics();
        if (store != null) {
            store.drawString(s, x, y);
            store.dispose();
        } // end if
        final Graphics2D g = slot.getBuffer().createGraphics();
        final FontMetrics fm = g.getFontMetrics(slot.getFont());
        final Rectangle2D r = fm.getStringBounds(s, g);
        g.dispose();
        slot.setX(x + (int) r.getWidth());
        return Value.UNIT;
    } // end method 'caml_gr_draw_string(CodeRunner, Value)'

    /**
     * Computes width and height of a given string according to current font.
     * @param ctxt context
     * @param str string to compute size from
     * @return <i>(string_width, string_height)</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_text_size(final CodeRunner ctxt,
                                          final Value str)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        final Graphics2D g = slot.getBuffer().createGraphics();
        final FontMetrics fm = g.getFontMetrics(slot.getFont());
        final Rectangle2D r = fm.getStringBounds(str.asBlock().asString(), g);
        g.dispose();
        final Block res =
            Block.createBlock(0,
                              Value.createFromLong((int) r.getWidth()),
                              Value.createFromLong((int) r.getHeight()));
        return Value.createFromBlock(res);
    } // end method 'caml_gr_text_size(CodeRunner, Value)'

} // end class 'Text'
